package edu.gdkm.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;

//读取资源文件
@PropertySource("classpath:db.properties")
public class DruidConfig {
    //自动将资源文件内容注入到env对象中
    @Autowired
    Environment env;
    //数据源对象
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setDriverClassName(env.getProperty("jdbc.driver"));
        druidDataSource.setUrl(env.getProperty("jdbc.url"));
        druidDataSource.setUsername(env.getProperty("jdbc.username"));
        druidDataSource.setPassword(env.getProperty("jdbc.password"));
        druidDataSource.setInitialSize(Integer.parseInt(env.getProperty("jdbc.initialSize")));
//        druidDataSource.setMaxIdle(Integer.parseInt(env.getProperty("jdbc.maxIdle")));
        //配置log4j日志过滤器
        druidDataSource.setFilters(env.getProperty("jdbc.filters"));
        return druidDataSource;
    }
    //事务管理对象
    @Bean
    public PlatformTransactionManager platformTransactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
